package com.lxl.hadoop.reverse;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.lib.input.FileSplit;

import java.io.IOException;

/**
 * @Author lixilin
 * @Date 2022/1/9 10:08
 * @Description
 */
public class WcMapper1 extends Mapper<LongWritable, Text,Text, NullWritable> {

    String fileName;

    @Override
    protected void setup(Context context) throws IOException, InterruptedException {
        // 获取切片信息
        FileSplit split = (FileSplit)context.getInputSplit();
        // 获取文件名称
        fileName = split.getPath().getName();
    }

    Text outKey = new Text();
    NullWritable outVal = NullWritable.get();
    StringBuilder kb = new StringBuilder();
    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {

        String[] words = value.toString().split("\t");
        for (String word : words) {
            kb.setLength(0);
            kb.append(word);
            kb.append("--");
            kb.append(fileName);
            outKey.set(kb.toString());
            context.write(outKey,outVal);
        }
    }
}
